class Reservation < ActiveRecord::Base

  validates_presence_of(:room_id, :guest_id, :message => "obrigatory fields")
  validates_numericality_of(:numAdults, :numChildren)
  validate :invalide_date, :message => "invalide date choice"
  validate :check_available_room
  
  def invalide_date
    date = Date.new
    if ( arriveDate.to_s > exitDate.to_s )
      errors.add("date choice")
    else
      if ( arriveDate.to_s < date.to_s)
        errors.add("date choice")
      end
    end
  end
  
  def check_available_room
    @available = []
    # pegar opcao do usuario
    arDate = :arriveDate
    exDate = :exitDate
    # pegar quartos do banco
    @reservations = Reservation.new
    @reservations = Reservation.all
    # pra cada quarto que a data nao conflita
    # adicionar em um vetor
    i = 0
    @reservations.each() do |reservation|
      if(reservation.room_id == :room_id)
        if ( exDate.to_s < reservation.arriveDate.to_s) || (arDate.to_s > reservation.exitDate.to_s)
        else
          errors.add("Already reserved.");
        end      
      end      
    end
  end
  
end
